package client.comm.baoding.ui

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Intent
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.api.bean.FzInter
import client.comm.baoding.api.bean.MeCoupon
import client.comm.baoding.api.bean.ShopingBean
import client.comm.baoding.api.bean.UserAddress
import client.comm.baoding.databinding.ActivityPayorderupimgBinding
import client.comm.baoding.databinding.LayoutConfirmenterorderitemBinding
import client.comm.baoding.databinding.LayoutSktyEeBinding
import client.comm.baoding.dialog.MsgTitleDialog
import client.comm.baoding.ui.vm.PayOrderUpimgViewModel
import client.comm.commlib.dialog.CameraOrSelectDialog
import client.comm.commlib.toast.toast
import client.comm.commlib.widget.EmptyLayout
import java.math.BigDecimal

class PayOrderUpimgActivity :
    BaseMiddleActivity<PayOrderUpimgViewModel, ActivityPayorderupimgBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_payorderupimg
    }

    override fun initViewModel(): PayOrderUpimgViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel

        binding.emptyLayout.setOnClickListener {
            viewModel.userInfo()
        }

    }

    override fun initData() {

        val list = intent.getParcelableArrayListExtra<ShopingBean>("list")
        list?.let {
            binding.goodsWrap.removeAllViews()
            for (data in it) {
                val bindingType =
                    DataBindingUtil.inflate<LayoutConfirmenterorderitemBinding>(
                        LayoutInflater.from(this),
                        R.layout.layout_confirmenterorderitem,
                        binding.goodsWrap,
                        false
                    ).apply {
                        this.bean = data
                        mImgLoader.load(data.goods_img).into(img)

                        commPriceIntegral.apply {
                            var is_price_show: Boolean = false
                            var is_integral_unit_show: Boolean = false

                            if (data.price > 0) {
                                tvPrice.text = "${data.price}"
                                is_price_show = true


                            } else {
                                tvPrice.text = ""
                                is_price_show = false

                            }

                            if (is_price_show) {
                                tvPriceUnit.visibility = View.VISIBLE
                            } else {
                                tvPriceUnit.visibility = View.GONE
                            }

                            if (is_integral_unit_show) {
                                tvIntegralDesc.visibility = View.VISIBLE
                            } else {
                                tvIntegralDesc.visibility = View.GONE
                            }

                            tvPrice.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16F)
                            tvIntegral.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16F)

                            tvPriceUnit.setTextColor(resources!!.getColor(R.color.txt_color))
                            tvPrice.setTextColor(resources!!.getColor(R.color.txt_color))
                            tvIntegral.setTextColor(resources!!.getColor(R.color.txt_color))
                            tvIntegralDesc.setTextColor(resources!!.getColor(R.color.txt_color))
                        }

                    }
                binding.goodsWrap.addView(bindingType.root)
            }
            updatePrice()
        }
        viewModel.userInfo()
    }

    fun updatePrice() {
        var number: BigDecimal = BigDecimal(0)
//        var lpq_price_new: BigDecimal = BigDecimal(0)
        var integral: BigDecimal = BigDecimal(0)
//        var integral_f: BigDecimal = BigDecimal(0)
        val items = intent.getParcelableArrayListExtra<ShopingBean>("list")
        items?.let {
            for (b in items) {
                b?.let {
                    number = number.add(BigDecimal(b.num).multiply(BigDecimal(b.price)))
                    if (b.integral > 0)
                        integral =
                            integral.add(BigDecimal(b.num).multiply(BigDecimal(b.integral)))
                }
            }
        }

        binding.incGoodsList.apply {
            this.price = "${number.toDouble()}"
            this.integral = "${integral.toDouble()}"
        }
        binding.price = "${number.toDouble()}"
        binding.integral = "${integral.toDouble()}"

        binding.commPriceIntegral1.apply {
            var is_price_show: Boolean = false
            var is_integral_unit_show: Boolean = false

            var integral = integral.toDouble()
            var price = number.toDouble()
//            var lpqPrice = lpq_price_new.toDouble()
            if (price > 0) {
                tvPrice.text = "${price}"
                is_price_show = true
                if (integral > 0) {
                    tvIntegral.text = "+" + integral //+ "积分"
                    is_integral_unit_show = true
                } else {
                    tvIntegral.text = ""
                    is_integral_unit_show = false
                }
            } else {
                tvPrice.text = ""
                is_price_show = false
                if (integral > 0) {
                    tvIntegral.text = "" + integral //+ "积分"
                    is_integral_unit_show = true
                } else {
                    tvIntegral.text = ""
                    is_integral_unit_show = false
                }
            }

            if (is_price_show) {
                tvPriceUnit.visibility = View.VISIBLE
            } else {
                tvPriceUnit.visibility = View.GONE
                binding.incGoodsList.rlGoodAccount.visibility = View.GONE
            }

            if (is_integral_unit_show) {
                tvIntegralDesc.visibility = View.VISIBLE
            } else {
                tvIntegralDesc.visibility = View.GONE
            }
        }

        binding.commPriceIntegral1.apply {
            var is_price_show: Boolean = false
            var is_integral_unit_show: Boolean = false

            var integral = integral.toDouble()
            var price = number.toDouble()

            if (price > 0) {
                tvPrice.text = "${price}"
                is_price_show = true
                if (integral > 0) {
                    tvIntegral.text = "+" + integral //+ "积分"
                    is_integral_unit_show = true
                } else {
                    tvIntegral.text = ""
                    is_integral_unit_show = false
                }
            } else {
                tvPrice.text = ""
                is_price_show = false
                if (integral > 0) {
                    tvIntegral.text = "" + integral //+ "积分"
                    is_integral_unit_show = true
                } else {
                    tvIntegral.text = ""
                    is_integral_unit_show = false
                }
            }


            if (is_price_show) {
                tvPriceUnit.visibility = View.VISIBLE
            } else {
                tvPriceUnit.visibility = View.GONE
            }

            if (is_integral_unit_show) {
                tvIntegralDesc.visibility = View.VISIBLE
            } else {
                tvIntegralDesc.visibility = View.GONE
            }
        }

    }

    override fun observerUI() {
        viewModel.userRet.observe(this, {

            it?.let {
                if (it.isSuccess) {

                    binding.emptyLayout.visibility = View.GONE

                    it.data?.address_list?.let {
                        if (it.size > 0)
                            binding.addr = it[0]
                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
        binding.addr = intent.getSerializableExtra("addr") as UserAddress.Address

        viewModel.okCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("上传成功")
                    finish()
                } else {
                    toast(it.message)
                }
            }
        })
        viewModel.cancleRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("撤销成功")
                    finish()
                } else {
                    toast(it.message)
                }
            }
        })
    }

    fun imgSelect() {

        CameraOrSelectDialog(this).apply {

            suc = {
                viewModel.imgOne.value = it
                mImgLoader.load(it).into(this@PayOrderUpimgActivity.binding.img)
            }
        }.show()
    }

    fun selectAddr() {
        startActivityForResult(Intent(this, AddrMangerActivity::class.java).apply {
            putExtra("select", true)
        }, 0x11)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            0x11 -> {
                if (resultCode == RESULT_OK) {
                    data?.let {
                        val addr = it.getSerializableExtra("address") as UserAddress.Address?
                        addr?.let {
                            binding.addr = it
                        }
                    }
                }
            }

        }
    }

    fun ok() {
        if (viewModel.imgOne.value == null) {
            toast("请选择图片后再上传")
            return
        }
        viewModel.ok(this)
    }

    fun cancle() {
        MsgTitleDialog(this).apply {
            title = "是否撤销订单?"
            cancleTxt = "关闭"
            okTxt = "撤销"
            ok = {
                viewModel.cancle()
            }
        }.show()
    }

    fun goRecord() {
        finish()
    }
}